package	com.mes.web.controller.jyy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.jyy.Approve;
import com.mes.entity.jyy.ApproveDetail;
import com.mes.service.jyy.ApproveDetailService;
import com.mes.service.jyy.ApproveService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
/**
 *
 *@ClassName: ApproveDetailController
 *@Description: 
 *@Author jyy
 *@Date 2019-07-22 17:27:25
 *@version 1.0
*/
@Controller
@RequestMapping("/approveDetail")
public class ApproveDetailController{
	@Resource ApproveDetailService approveDetailService;
	@Resource ApproveService approveService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageApproveDetail (Model model) {
		return "/td/ApproveDetail";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public ApproveDetail queryById(Long id) {
		ApproveDetail param = approveDetailService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<ApproveDetail> queryAllByMution(ApproveDetail param) {
		List<ApproveDetail> list = approveDetailService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(ApproveDetail param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<ApproveDetail> list = approveDetailService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = approveDetailService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(ApproveDetail param, HttpServletResponse response) throws Exception{
		Long id = approveDetailService.insert(param);
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(ApproveDetail param,HttpServletResponse response) throws Exception{
        //当前时间
        SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String   date   =   sDateFormat.format(new Date());
		//审批时间
        param.setDatetimes(date);
        Integer count = approveDetailService.update(param);
		if(count > 0){
			param = approveDetailService.queryById(param.getId());
			//详情对应的审批
			Approve approve = approveService.queryById(param.getAid());
			//如果是审批通过  反之审批驳回
			if("1".equals(param.getState())){
				ApproveDetail xa = new ApproveDetail();
				xa.setAid(param.getAid());
				xa.setGrade(param.getGrade()+1);
				List<ApproveDetail> xaList = approveDetailService.queryAllByMution(xa);
				//审批通过 判断是否有下级未审批，如有审批中下级为待审批  反之审批通过
				/** 审批状态  0待审批 1 审批中 2审批通过  3审批驳回*/
				if (xaList.size() > 0) {
					//如果是一级审批修改状态为审批中
					if(param.getGrade() == 1){
						approve.setState(1);
						approveService.update(approve);
						approveService.approvalBilSqll(approve.getId(), approve.getType(),approve.getState());
					}
					
					//将下一级修改为待审批
					xa = xaList.get(0);
					/** 审批状态  -1未审批，0待审批，1审批通过 2审批驳回*/
					xa.setState("0");
					approveDetailService.update(xa);
				}else{
					approve.setState(2);
					approveService.update(approve);
					approveService.approvalBilSqll(approve.getId(), approve.getType(),approve.getState());
				}
			}else if("2".equals(param.getState())){
				/** 审批状态  0待审批 1 审批中 2审批通过  3审批驳回*/
				approve.setState(3);
				approveService.update(approve);
				approveService.approvalBilSqll(approve.getId(), approve.getType(),approve.getState());
			}
		}
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = approveDetailService.delete(ids);
		response.getWriter().print(count);
	}
}
